package itsm.isperp.module.controller.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.utils.HibernateUUIDGenerator;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppPortal;
import itsm.isperp.module.entity.app.AppRole;
import itsm.isperp.module.service.app.AppPortalPersonConfigService;
import itsm.isperp.module.service.app.AppPortalRoleConfigService;
import itsm.isperp.module.service.app.AppPortalService;
import itsm.isperp.module.service.app.AppRoleService;
import itsm.isperp.report.Counter;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.abel533.echarts.Option;
import com.github.abel533.echarts.code.Orient;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Pie;
import com.github.abel533.echarts.series.Series;

/**
 * 
 * @author lizx
 * @date 2014-3-24
 */
@Controller
@RequestMapping("app/portal")
public class AppPortalController extends BaseController<AppPortal> {

	@Autowired
	protected AppPortalService appPortalService;

	@Override
	public AppPortalService getService() {
		return this.appPortalService;
	}

	@Override
	public String getPageTitle() {
		return "系统门户";
	}
	
	@RequestMapping("indexView")
  public ModelAndView index(String id, HttpServletRequest req)
      throws Exception {
    ModelAndView mv = new ModelAndView("app/portal/index");
    mv.addObject("username", ContextHolder.getLoginUsername());

    return mv;
  }
	

	@RequestMapping("edit")
	public ModelAndView edit(String id, HttpServletRequest req) throws Exception {
		ModelAndView mv = this.getModelAndView();
		AppPortal model = null;
		if (StringUtils.isEmpty(id)) {
			model = new AppPortal();
			model.setId(HibernateUUIDGenerator.generate());
		} else {
			model = this.getService().findOneDetail(id);
		}

		AppRoleService appRoleService = ContextHolder.getSpringBean(AppRoleService.class);
		List<AppRole> allRole = appRoleService.findAllRole();

		mv.addObject("roles", allRole);
		mv.addObject("model", model);
		return mv;
	}

	@RequestMapping("rolePortalConfigEdit")
	public ModelAndView rolePortalConfigEdit(String roleId) {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("roleId", roleId);
		return mv;
	}

	@RequestMapping("rolePortals")
	@ResponseBody
	public List<AppPortal> rolePortals(String roleId) {
		return this.getService().findRolePortals(roleId);

	}

	@RequestMapping("saveRolePortalConfig")
	@ResponseBody
	public ResultMessage saveRolePortalConfig(String roleId, String[] protals, String[] indexs) {

		AppPortalRoleConfigService appPortalRoleConfigService = ContextHolder.getSpringBean(AppPortalRoleConfigService.class);

		appPortalRoleConfigService.saveRoleConfig(roleId, protals, indexs);

		return ResultMessage.success();
	}

	@RequestMapping("savePersonPortalConfig")
	@ResponseBody
	public ResultMessage savePersonPortalConfig(String[] protals, String[] indexs) {

		AppPortalPersonConfigService appPortalPersonConfigService = ContextHolder.getSpringBean(AppPortalPersonConfigService.class);
		String username = ContextHolder.getLoginUsername();
		appPortalPersonConfigService.savePersonPortalConfig(username, protals, indexs);

		return ResultMessage.success();
	}

	@RequestMapping("personView")
	public ModelAndView personView() {
		ModelAndView mv = this.getModelAndView();
		String username = ContextHolder.getLoginUsername();
		mv.addObject("userId", username);
		return mv;
	}

	@RequestMapping("personPortals")
	@ResponseBody
	public List<AppPortal> personPortals() {
		String username = ContextHolder.getLoginUsername();
		return this.getService().findPersonPortals(username);

	}

	

}
